import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { floodsub } from '@libp2p/floodsub'
// import { gossipsub } from '@chainsafe/libp2p-gossipsub'
// import { gossipsub } from './gossipsub/index.js'
import { mplex } from '@libp2p/mplex'
import { webSockets } from '@libp2p/websockets'
import { createLibp2p } from 'libp2p'
import { identify } from '@libp2p/identify'
import { preSharedKey } from '@libp2p/pnet'
import { createEd25519PeerId } from './peer-id.js'

export const createNode = async ({ swarmKey, seed }) => {
    const peerId = await createEd25519PeerId(seed)
    const node = await createLibp2p({
        peerId,
        addresses: {
            listen: [
                '/ip4/0.0.0.0/tcp/4008/ws',
            ]
        },
        transports: [webSockets()],
        streamMuxers: [yamux(), mplex()],
        connectionEncryption: [noise()],
        services: {
            pubsub: floodsub(),
            // pubsub: gossipsub({ allowPublishToZeroPeers: true, emitSelf: false }),
            identify: identify(),
            // relay: circuitRelayServer()
        },
        connectionProtector: preSharedKey({
            psk: swarmKey
        }),
        connectionManager: {
            minConnections: 500,
            maxConnections: 1000
        }
    })

    return node
}
